我的Bilibili频道:香芋派Taro
我的个人博客:taropie0224.github.io(阅读体验更佳)
我的公众号:香芋派的烘焙坊
我的音频技术交流群:1136403177
我的个人微信:JazzyTaroPie
https://leetcode.cn/problems/merge-k-sorted-lists/
题解and思路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Solution { public: ListNode* mergeKLists(vector<ListNode*>& lists) { if (lists.empty() == true) return 0;
vector<int> vec; for (int i = 0; i < lists.size(); i++) { ListNode* node = lists[i]; while (node != nullptr) { vec.push_back(node->val); node = node->next; } } sort(vec.begin(), vec.end());
ListNode* head = new ListNode(); ListNode* node = head; for (int i = 0; i < vec.size(); i++) { ListNode* cur = new ListNode(); cur->val = vec[i]; node->next = cur; node = cur; } return head->next; } };
|